﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.Options;

namespace IdentityManagement
{
    public class XmsIdentityOptionsManager : AbpDynamicOptionsManager<IdentityOptions>
    {
        protected IdentitySettings IdentitySettings { get; }

        public XmsIdentityOptionsManager(IOptionsFactory<IdentityOptions> factory,
            IdentitySettings identitySettings)
            : base(factory)
        {
            IdentitySettings = identitySettings;
        }

        protected override async Task OverrideOptionsAsync(string name, IdentityOptions options)
        {
            options.Password.RequiredLength = IdentitySettings.PasswordRequiredLength;
            options.Password.RequiredUniqueChars = IdentitySettings.PasswordRequiredUniqueChars;
            options.Password.RequireNonAlphanumeric = IdentitySettings.PasswordRequireNonAlphanumeric;
            options.Password.RequireLowercase = IdentitySettings.PasswordRequireLowercase;
            options.Password.RequireUppercase = IdentitySettings.PasswordRequireUppercase;
            options.Password.RequireDigit = IdentitySettings.PasswordRequireDigit;

            options.Lockout.AllowedForNewUsers = IdentitySettings.LockoutAllowedForNewUsers;
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromSeconds(IdentitySettings.LockoutLockoutDuration);
            options.Lockout.MaxFailedAccessAttempts = IdentitySettings.LockoutMaxFailedAccessAttempts;

            options.SignIn.RequireConfirmedEmail = IdentitySettings.SignInRequireConfirmedEmail;
            options.SignIn.RequireConfirmedPhoneNumber = IdentitySettings.SignInRequireConfirmedPhoneNumber;
            await Task.CompletedTask;
        }
    }
}
